package znbkListCtfd.presenter;

import android.util.Log;

import znbkListCtfd.api.WrongTopicTutoringListApi;
import znbkListCtfd.entity.WrongTopicTutoringListEntity;
import znbkListCtfd.IWrongTopicTutoringPresenter;
import com.example.lancooznbk.view.base.BaseFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import znbkListCtfd.fargment.WrongTopicTutoringListFragment;
import com.lancoo.rxretrofitlibrary.http.HttpManager;
import com.lancoo.rxretrofitlibrary.listener.HttpOnNextListener;
import com.lancoo.rxretrofitlibrary.subscribers.ProgressSubscriber;

import java.util.List;

import znbkTestPaperCtfd.activity.WrongTopicTutoringPaperActivity;
import znbkTestPaperInfor.ZnbkPaperDef;
import znbkTestPaperLstjt.activity.HistoryRecommendQuesPaperActivity;

/**
 * Created by Administrator on 2018\2\1 0001.
 */

public class WrongTopicTutoringListPresenter implements IWrongTopicTutoringPresenter {
    private ILoadMoreDataView mView;
    private int pageIndex = 1;
    private int quesStatus = 0;//默认是未完成
    private String quesType = "";
    private int lastPosition = 0;

//    private int wrongFrom;
    private ProgressSubscriber mSubscriber;
    private HttpOnNextListener<WrongTopicTutoringListEntity> httpOnNextListener = new HttpOnNextListener<WrongTopicTutoringListEntity>() {
        @Override
        public void onNext(WrongTopicTutoringListEntity wrongTopicTutoringListEntity) {
            if (wrongTopicTutoringListEntity.getTotalQues()> 0) {
                mView.hideLoading();
                List<WrongTopicTutoringListEntity.ParrentQuesListBean> parrentQuesList = wrongTopicTutoringListEntity.getParrentQuesList();
                for (int i = 0 ; i < parrentQuesList.size() ; i ++){ //预处理题库返回不合理的地方
                    String summary = parrentQuesList.get(i).getTKQue().getQuesBrief();
                    /*summary = TagHelperUtils.replacAllTag(summary,"<P>","");
                    summary = TagHelperUtils.replacAllTag(summary,"<BR>","");
                    summary = TagHelperUtils.replaceNULL_U_Tag(summary);
                    summary = TagHelperUtils.replaceImageTag(summary);
                    if (summary.length() >100 &&
                            !(summary.contains("<table") || summary.contains("<TABLE"))){
                        summary = summary.substring(0,100);
                    }*/

                    if(summary!=null) {
                        String regexStr = "<[^>]*>"; //去除所有的标签
                        summary = summary.replaceAll(regexStr, "");
                        if (summary.length() > 100) {
                            summary = summary.substring(0, 100);
                        }
                    }else{
                        summary = "已删除";
                    }
                    parrentQuesList.get(i).getTKQue().setQuesBrief(summary);
                }
                if (pageIndex == 1) {
                    mView.loadData(wrongTopicTutoringListEntity);
                    lastPosition = 0;
                } else {
                    mView.loadMoreData(wrongTopicTutoringListEntity);
                }

            }else {
                mView.showNoData("暂无错题辅导题目");
            }
        }


        @Override
        public void onError(Throwable e) {
            super.onError(e);
            mView.showNetError();
        }
    };

    public WrongTopicTutoringListPresenter(ILoadMoreDataView mView) {
        this.mView = mView;
//        this.wrongFrom = wrongFrom;
    }

    private void getData(){
        if (mSubscriber != null && !mSubscriber.isUnsubscribed()){
            mSubscriber.unsubscribe();
        }
        WrongTopicTutoringListApi api = new WrongTopicTutoringListApi(httpOnNextListener,(WrongTopicTutoringListFragment)mView);

        api.setQuesType(quesType);
        api.setAnswerState(quesStatus);
        api.setPageNumber(pageIndex);
        api.setPageSize(lastPosition + 10);
        mSubscriber = new ProgressSubscriber(api);
        HttpManager.getInstance().doHttpDeal(api,mSubscriber);
    }
    @Override
    public void getData(boolean isShowing) {
        if (isShowing) mView.showLoading();
        pageIndex = 1;
        getData();
    }

    @Override
    public void getMoreData() {
        pageIndex = pageIndex + 1;
        getData();
    }

    @Override
    public void getDataByStatus(int quesStatusId) {
//        if (quesStatus == quesStatusId) return;
        quesStatus = quesStatusId;
        quesType = "";
        getData(false);
    }

    @Override
    public void getDataByQuesType(String quesTypeId) {
//        if (quesType.equals(quesTypeId)) return;
        quesType = quesTypeId;
        getData(false);

    }


    public void openItemDetail(int position, WrongTopicTutoringListEntity.ParrentQuesListBean parrentQuesListBean) {
        lastPosition = position;
//        //ToastUtils.showShort("点击第"+(position+1)+"个");

//        UserInfo.PaperStatus = modelListBean.getState();
//        UserInfo.PaperFrom = modelListBean.getQuesFrom();
//        //TODO 调用下面方法进行跳转
//        PaperInfor.ModelFrom = 1;//错题
//        @PaperDef.PaperFrom int from;
//        if (modelListBean.getQuesFrom() == 0)
//            from = PaperDef.FROM_BASE_REVIEW_WRONG;
//        else
//            from = PaperDef.FROM_DIFFERENT_REVIEW_WRONG;
//
//        WrongQuesPaperActivity.launch((BaseFragment) mView,modelListBean,from);


        WrongTopicTutoringPaperActivity.launch((BaseFragment)mView,parrentQuesListBean,
                ZnbkPaperDef.FROM_WRONG_TOPIC_TUTORING);
        Log.e("========QuesID======",""+parrentQuesListBean.getTKQue().getQuesID());
        Log.e("========ItemFrom======",""+parrentQuesListBean.getItemFrom());
        Log.e("======UpdateTime====",""+parrentQuesListBean.getUpdateTime());

    }


    public void openItemHistoryDetail(int position, WrongTopicTutoringListEntity.ParrentQuesListBean parrentQuesListBean) {
        lastPosition = position;
//        ToastUtils.showShort("历史推荐题  position: "+position);

//        UserInfo.PaperStatus = modelListBean.getState();
//        UserInfo.PaperFrom = modelListBean.getQuesFrom();
//        @PaperDef.PaperFrom int from;
//        if (modelListBean.getQuesFrom() == 0)
//            from = PaperDef.FROM_BASE_REVIEW_WRONG_History;
//        else
//            from = PaperDef.FROM_DIFFERENT_REVIEW_WRONG_History;
//
//        //可以自己预存一些变量 然后进行跳转
        HistoryRecommendQuesPaperActivity.launch((BaseFragment) mView,parrentQuesListBean,ZnbkPaperDef.FROM_HISTORY_RECOMMEND_QUESTION);
    }
}